<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>鼠标位置-拖拽</title>
    <style>
        #outer{
            width: 150px;
            position: absolute;
        }
    </style>
</head>
<body>
    <div id="outer">
        <img src="../imgs/我老婆.webp" width="150px">
    </div>

    <script>
        //div距离浏览器边框的距离
        let mdx=0;
        let mdy=0;

        //鼠标距div边框的距离
        let dx=0;
        let dy=0;

        let flag = false;

        let outer = document.getElementById("outer");
        
        outer.onmousedown = function(event) {
            mdx = outer.offsetLeft;
            mdy = outer.offsetTop;

            let e = event || window.event;

            dx = e.clientX - mdx;
            dy = e.clientY -mdy;

            flag = true;
        }

        document.onmousemove = function(event) {
            if(flag) {
                let e = event || window.event;
                outer.style.left = (e.clientX - dx)+"px";
                outer.style.top = (e.clientY - dy)+"px";
            }
        }

        document.onmouseup = function() {
            flag = false;
        }
    </script>
</body>
</html>